package top.fengleifeng.question;

/**
 * @author : fengleifeng
 * @Package top.fengleifeng.question
 * @Description:
 * @date Date : 2021年01月05日 10:50 上午
 */
public class Num43字符串相乘 {

    class Solution {

        public String multiply(String num1, String num2) {
            char[] num1Arr = num1.length() >= num2.length() ? num1.toCharArray() : num2.toCharArray();
            char[] num2Arr = num1.length() >= num2.length() ? num2.toCharArray() : num1.toCharArray();
            int num1length = num1Arr.length;
            int num2length = num2Arr.length;
            StringBuffer sb = new StringBuffer("");
            for (int i = num2length - 1; i >= 0; i--) {
                String mul = mul(num1Arr, num2Arr[i]);
                sb = new StringBuffer(add(sb.toString(), mul, num2length - i - 1));
            }
            if(sb.length()>0){
                while (sb.charAt(0)=='0'){
                    sb.deleteCharAt(sb.length()-1);
                    if(sb.length()<=0){
                        return "0";
                    }
                }
            }
            return sb.toString();
        }

        private String mul(char[] num1Arr, char num) {
            StringBuffer sb = new StringBuffer("");
            int numNow = num - '0';
            int stepNum = 0;
            for (int i = num1Arr.length - 1; i >= 0; i--) {
                int k1 = num1Arr[i] - '0';
                int numX = k1 * numNow + stepNum;
                int addNum = numX % 10;
                stepNum = numX / 10;
                sb.append(addNum);
            }
            if (stepNum != 0) {
                sb.append(stepNum);
            }
            return sb.reverse().toString();
        }

        private String add(String num1, String num2, int index) {
            char[] num1Arr = num1.toCharArray();
            char[] num2Arr = num2.toCharArray();
            int length1All = num1.length();
            int length2All = num2.length();
            int length1 = length1All - index - 1;
            int length2 = length2All - 1;
            int stepNum = 0;
            StringBuffer sb = new StringBuffer("");
            if (index > 0) {
                for (int i = length1All - 1; i > length1All - 1 - index; i--) {
                    sb.append(num1Arr[i]);
                }
            }
            while (length1 >= 0 || length2 >= 0) {
                if (length1 >= 0 && length2 >= 0) {
                    int k1 = num1Arr[length1] - '0';
                    int k2 = num2Arr[length2] - '0';
                    int numX = k1 + k2 + stepNum;
                    int addNum = numX % 10;
                    stepNum = numX / 10;
                    sb.append(addNum);
                    length1--;
                    length2--;
                } else if (length1 >= 0) {
                    int k1 = num1Arr[length1] - '0';
                    int numX = k1 + stepNum;
                    int addNum = numX % 10;
                    stepNum = numX / 10;
                    sb.append(addNum);
                    length1--;
                } else if (length2 >= 0) {
                    int k2 = num2Arr[length2] - '0';
                    int numX = k2 + stepNum;
                    int addNum = numX % 10;
                    stepNum = numX / 10;
                    sb.append(addNum);
                    length2--;
                }
            }
            if (stepNum != 0) {
                sb.append(stepNum);
            }
            return sb.reverse().toString();
        }
    }


    public static void main(String[] args) {
        String s = "408";
        String s1 = "5";
        String multiply = new Num43字符串相乘().new Solution().multiply(s, s1);
        System.out.println(multiply);

//        String multiply1 = new Num43字符串相乘().new Solution().add(s, s1, 0);
//        System.out.println(multiply1);

//        String multiply12 = new Num43字符串相乘().new Solution().mul(s1.toCharArray(), '2');
//        System.out.println(multiply12);
    }
}
